﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 执行CMD语句
        /// </summary>
        /// <param name="cmd">要执行的CMD命令</param>
        public string RunCmd(string cmd)
        {
            var proc = new Process();
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.FileName = "cmd.exe";
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.RedirectStandardInput = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.Start();
            proc.StandardInput.WriteLine(cmd);
            proc.StandardInput.WriteLine("exit");
            string outStr = proc.StandardOutput.ReadToEnd();
            proc.Close();
            return outStr;
        }
        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            textBox1.Text = string.Empty;

            string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
            if (s == null || s.Length <= 0)
                return;

            string sFoder = s[0];
            
            string cmdstr = RunCmd(Environment.CurrentDirectory + "\\identify.exe" + " -verbose \"" + sFoder + "\"");
            string[] sArray;
            
            if (cmdstr.Contains("Format"))
            {
                sArray = cmdstr.Split(new string[] { "Format:", "Mime type" }, StringSplitOptions.RemoveEmptyEntries);
                textBox1.Text += "格式:" + sArray[1] + Environment.NewLine;
            }
            else
            {
                textBox1.Text = cmdstr;
            }
            if (cmdstr.Contains("Geometry"))
            {
                sArray = cmdstr.Split(new string[] { "Geometry:", "Units" }, StringSplitOptions.RemoveEmptyEntries);
                textBox1.Text += "分辨率:" + sArray[1] + Environment.NewLine;

            }

            if (cmdstr.Contains("Filesize"))
            {
                sArray = cmdstr.Split(new string[] { "Filesize:", "Number pixels" }, StringSplitOptions.RemoveEmptyEntries);
                if (sArray[1].Contains("B"))
                {
                    try
                    {
                        int bnum = int.Parse(sArray[1].Replace("B", ""));
                        bnum = bnum / 1000;
                        textBox1.Text += "大小:" + bnum + "K" + Environment.NewLine + Environment.NewLine;
                    }
                    catch (Exception)
                    {
                        textBox1.Text += "大小:" + sArray[1] + Environment.NewLine;
                    }
                }
                else
                {
                    textBox1.Text += "大小:" + sArray[1] + Environment.NewLine;

                }
            }

            if (cmdstr.Contains("Quality"))
            {
                sArray = cmdstr.Split(new string[] { "Quality:", "Orientation" }, StringSplitOptions.RemoveEmptyEntries);
                textBox1.Text += "质量:" + sArray[1] + Environment.NewLine;
            }

            if (cmdstr.Contains("Image:"))
            {
                sArray = cmdstr.Split(new string[] { "Image:", "Version" }, StringSplitOptions.RemoveEmptyEntries);
                textBox1.Text += "详细:" + sArray[1] + Environment.NewLine;
            }
        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.All;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://download.imagemagick.org/ImageMagick/download/binaries/ImageMagick-7.1.0-portable-Q16-x64.zip");
        }
    }
}
